package com.fuyin.controller;

import com.fuyin.commons.aop.RequestLogAnnotation;
import com.fuyin.commons.configuration.PropertyPlaceholderConfig;
import com.fuyin.commons.response.BaseResponse;
import com.fuyin.entity.GpEntity;
import com.fuyin.service.FileServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * @author yuanxilan (yuanxilan@keep.com)
 * @Description
 * @date 2021年12月04日 11:51 PM
 * @since
 */
@Api("文件接口")
@Controller
@Import(PropertyPlaceholderConfig.class)
@RequestMapping(value = "/file")
@Slf4j
public class FileController {

    @Autowired
    private FileServiceImpl fileService;

    //    @PostMapping(value = "/upload-excel", produces = "multipart/form-data")
    @PostMapping(value = "/upload-excel")
    @ResponseBody
    @ApiOperation("上传 Excel 文件")
    @ApiResponses(value = {
            @ApiResponse(code = 400, message = "参数不全"),
            @ApiResponse(code = 425, message = "文件格式错误，建议在模版文件中修改"),
            @ApiResponse(code = 426, message = "文件行数不符合要求")
    })
    @RequestLogAnnotation
    public BaseResponse<ArrayList<GpEntity>> uploadExcel(@RequestParam("type") Integer type, MultipartHttpServletRequest request) {
        MultipartFile file = request.getFile("file");
        return fileService.uploadExcel(type, file);
    }

    @GetMapping("/download-excel")
    @RequestLogAnnotation
    public void downloadExcel(@RequestParam("type") Integer type, HttpServletResponse response, @RequestParam("gpName") String gpName) {
        fileService.downloadExcel(type, response, gpName);
    }

//    @Data
//    public static class FileUploadDto {
//
//        @ApiModelProperty("文件类型(0-主要指标，1-利润表，2-现金流量表，3-资产负债表)")
//        private Integer type;
//
//        @ApiModelProperty("文件")
//        private MultipartFile file;
//    }
}
